//get select dict
/*
 * @transParams 对应参数表中的需要查询的字段名称 例如dic_mainAttachedFlag
 */
export function getSelectDict(transParams, obj) {
  return new Promise((resolve) => {
    let dictData = window.sessionStorage.getItem(transParams)
    if (dictData) { // 是否缓存
      resolve(JSON.parse(dictData))
    } else {
      // let rows = []
      if (transParams.indexOf('/') != -1) { // 请求后台
        let params = {}
        if (obj) {
          params = Object.assign({}, obj)
        }
        this.$http({
          url: this.$http.adornUrl('/api/sysDictDropdown'),
          method: 'post',
          data: this.$http.adornData(params)
        }).then(({data}) => {
          if (data && data.code === '000000') {
            let rows = [];
            if (data.rows && data.rows.length > 0) {
              rows = data.rows
              window.sessionStorage.setItem(transParams, JSON.stringify(rows))
            }
            resolve(rows)
          }
        })
      } else { // 请求参数表
        const params = {
          groupCodes: transParams
        }
        this.$http({
          url: this.$http.adornUrl('/api/sysDictDropdown'),
          method: 'post',
          data: this.$http.adornData(params)
        }).then(({data}) => {
          if (data && data.code === '000000') {
            let rows = [];
            if (data.rows && data.rows.length > 0) {
              rows = data.rows
              window.sessionStorage.setItem(transParams, JSON.stringify(rows))
            }
            resolve(rows)
          }
        })
      }
    }
  })
}

//get table list 获取表格数据
/*
*@url 请求地址
*@params 所传参数
* params : object {
  * isTrans: true,//是否需要翻译数据字典
  * transParams : ['dic_costCategory'],//查找数据字典所需参数
  * transDict : ['feeType'],//需要翻译key
  *  }
 */
export function getTableList(url, params, success) {
  let sessionDatas, rows
  return new Promise((resolve) => {
    this.$http({
      url: this.$http.adornUrl(url),
      method: 'post',
      data: this.$http.adornData(params)
    }).then(({data}) => {
      if (data.code != '000000') {
        this.$message.error(data.msg)
        resolve(success(data || data))
      } else if (params.isTrans) { // 需要翻译
        if (data.rows) {
          rows = data.rows //获取数据
        } else {
          rows = []
        }
        params.transParams.forEach((transItem, p) => {
          sessionDatas = JSON.parse(window.sessionStorage.getItem(transItem))
          if (sessionDatas) { // 有缓存
            rows.forEach((rowItem) => {
              let transDictName = params.transDict[p] + 'Desc'
              let descOk = 0;
              sessionDatas.forEach((dictItem) => {
                if (rowItem[params.transDict[p]] == dictItem['dictValue']) {
                  rowItem[transDictName] = dictItem['dictName']
                  descOk = 1;
                  return;
                }
              })
              if (descOk == 0) {
                rowItem[transDictName] = rowItem[params.transDict[p]]
              }
            })
          } else { // 无缓存 请求参数表
            let obj = {
              groupCodes: transItem
            }
            this.$http({
              url: this.$http.adornUrl('/api/sysDictDropdown'),
              method: 'post',
              data: this.$http.adornData(obj)
            }).then(({data}) => {
              if (data && data.code === '000000') {
                let dictrows = [];
                if (data.rows && data.rows.length > 0) {
                  dictrows = data.rows
                  window.sessionStorage.setItem(transItem, JSON.stringify(dictrows))
                }
                sessionDatas = dictrows
                rows.forEach((rowItem) => {
                  let transDictName = params.transDict[p] + 'Desc'
                  let descOk = 0;
                  sessionDatas.forEach((dictItem) => {
                    if (rowItem[params.transDict[p]] == dictItem['dictValue']) {
                      rowItem[transDictName] = dictItem['dictName']
                      descOk = 1;
                      return;
                    }
                  })
                  if (descOk == 0) {
                    rowItem[transDictName] = rowItem[params.transDict[p]]
                  }
                })
              }
            })
          }
        })
        setTimeout(() => {
          data.rows = rows
          resolve(success(data || data))
        }, 500)
      } else { // 不需要翻译
        if (success) {
          resolve(success(data || data))
        } else {
          resolve(data)
        }
      }
    }, err => {
      console.info("error-----")
      console.info(err)
      //this.$message.error(err.rsmsg)
      console.info(err.state)
    })
  })
}

export function getDownload (url, params, callback) {
  return new Promise((resolve) => {
    this.$http({
      url: this.$http.adornUrl(url),
      method: 'get',
      params: this.$http.adornParams(params),
      responseType: 'arraybuffer'
    }).then(({data}) => {
      resolve(callback(data || data))
    })
  })
}
